package com.sky.mapper;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderPageQueryVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    /**
     * 存储简单订单
     * @param orders
     */
    void addOrder(Orders orders);

    /**
     * 根据订单号查询当前用户的订单
     * @param outTradeNo
     * @param userId
     * @return
     */
    @Select("select *from orders where user_id = #{userId} and number = #{outTradeNo}")
    Orders getByNumberAndUserId(String outTradeNo, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 通过订单状态和最后截至时间获取订单
     * @param sourceStatus
     * @param finalTime
     * @return
     */
    @Select("select *from orders where status = #{sourceStatus} and order_time < #{finalTime}")
    List<Orders> getOrderByStatusAndFinalTime(Integer sourceStatus, LocalDateTime finalTime);

    /**
     * 获取订单列表
     *
     * @param ordersPageQueryDTO
     * @return
     */
    List<OrderPageQueryVO> getPageQueryOrders(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 通过id获取订单
     * @param id
     * @return
     */
    @Select("select id, number, status, user_id, address_book_id, order_time, checkout_time, " +
            "pay_method, pay_status, amount, remark, phone, address, user_name, consignee, " +
            "cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, " +
            "delivery_time, pack_amount, tableware_number, tableware_status " +
            "from orders where id = #{id}")
    Orders getOrderById(Long id);

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @Select("select count(*) from orders where status = #{pendingPayment}")
    int countByStatus(Integer pendingPayment);

    /**
     * 营业额统计
     * @param map
     * @return
     */
    Double sumAmountByTime(Map map);

    /**
     * 订单统计
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
